//
//  ICMessageForwardSearchController.m
//  ICome
//
//  Created by zhangrongwu on 2017/8/22.
//  Copyright © 2017年 iCom. All rights reserved.
//

#import "ICMessageForwardSearchController.h"
#import "ICTitleView.h"
#import "ICMessageDatabase.h"
#import "ICChatForwardCell.h"


@interface ICMessageForwardSearchController ()<UITableViewDataSource,UITableViewDelegate,ICTitleViewDelegate>
@property (nonatomic, weak) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataList;

@property (nonatomic, strong) UILabel * emptyStyleLable;

@property (nonatomic, strong) ICTitleView *titleView;

@end

@implementation ICMessageForwardSearchController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setupSubView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark - Table view data source | delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ICChatForwardCell *cell = [ICChatForwardCell cellWithTableView:tableView];
    cell.group = self.dataList[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ICGroup *group = self.dataList[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(groupSearch:)]) {
        [self.delegate groupSearch:group];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIView*)findFirstResponderBeneathView:(UIView*)view
{
    // Search recursively for first responder
    for ( UIView *childView in view.subviews ) {
        if ( [childView respondsToSelector:@selector(isFirstResponder)] && [childView isFirstResponder] )
            return childView;
        UIView *result = [self findFirstResponderBeneathView:childView];
        if ( result )
            return result;
    }
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55.0f * Level;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.titleView.searchBar resignFirstResponder];
}

#pragma mark - Private

- (void)setupSubView
{
    self.view.backgroundColor = BACKGROUNDCOLOR;
    self.navigationController.navigationBarHidden = YES;
    self.titleView = [[ICTitleView alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width, [ICTools getNavbarHeight]+[ICTools getStatusbarHeight])];
    self.titleView.delegate     = self;
    self.titleView.backgroundColor  = NE_BACKGROUND_COLOR;
    self.titleView.searchBar.backgroundColor = [ICTools getRgbColor:0x5CACFE];
    self.titleView.searchBar.tintColor = [UIColor whiteColor];
    self.titleView.searchBar.textColor = [UIColor whiteColor];
    [self.titleView.searchBar setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    UIImage *image = [UIImage  imageNamedFromiComeSDKBundle:@"icon_search"];
    self.titleView.searchBar.iconImage.image = [image imageWithColor:[UIColor whiteColor]];
    [self.view addSubview:self.titleView];
    
    UITableView *tableView = [[UITableView alloc] init];
    tableView.frame        = CGRectMake(0, self.titleView.bottom, self.view.width, self.view.height-self.titleView.height);
    tableView.delegate     = self;
    tableView.dataSource   = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:tableView];
    self.tableView = tableView;
}

#pragma mark - titleViewDelegate

- (void)cancelBtnClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)searchText:(NSString *)text
{
    [self.dataList removeAllObjects];
    [self.emptyStyleLable removeFromSuperview];
    [self.tableView reloadData];
    if (nil == text || 0 == text.length) return;
    
    WEAKSELF
    NSDictionary *param = @{@"key":text,
                            @"type":@"1"};
    NSLog(@"测试");
    [[ICSocketRequestManager sharedInstance] searchEmpOROrgWithObject:param Success:^(id object) {
       
        [weakSelf.dataList removeAllObjects];
        [weakSelf.emptyStyleLable removeFromSuperview];
      
        NSArray *array = [object objectForKey:@"data"];
        if (array.count) {
            
            for (NSDictionary *dic in array) {
                ICGroup *group = [[ICGroup alloc] init];
                group.gId = dic[@"eId"];
                group.gName = dic[@"eName"];
                group.photoId = dic[@"photoId"];
                [weakSelf.dataList addObject:group];
            }
        } else {
            [weakSelf.tableView addSubview:weakSelf.emptyStyleLable];
        }
        [weakSelf.tableView reloadData];
    } Failure:^(NSError *error, id other) {
        [MBProgressHUD showError:error.domain];
    }];
}

#pragma mark - Getter

- (NSMutableArray *)dataList
{
    if (!_dataList) {
        _dataList = [[NSMutableArray alloc] init];
    }
    return _dataList;
}

- (UILabel *)emptyStyleLable
{
    if (!_emptyStyleLable) {
        _emptyStyleLable       = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, App_Frame_Width, 30)];
        _emptyStyleLable.font  = [UIFont systemFontOfSize:17];
        _emptyStyleLable.backgroundColor = [UIColor clearColor];
        _emptyStyleLable.textAlignment = NSTextAlignmentCenter;
        _emptyStyleLable.textColor     = ICRGB(0x9a9a9a);
        _emptyStyleLable.text = @"暂无搜索结果";
        [self.view addSubview:_emptyStyleLable];
    }
    return _emptyStyleLable;
}
@end
